package algorithm;

import java.util.Map;

import data.Option;

public abstract class AbstractCounterBased implements Algorithm {

	protected Map<Object, Double> result;
	protected String name;
	protected long size;
//	protected double confidence = 1.0;
//	protected double epsilon = 0.0;
	
	protected AbstractCounterBased(String name){
		this.name = name;
	}
	
	@Override
	public void run(int[] stream, int maxScale) {
		Runtime runtime = Runtime.getRuntime();
		if (stream != null){

			runtime.gc();
			long totalMemory = runtime.totalMemory();
			
			System.out.println(name + " algo:");
			long start = System.currentTimeMillis();
			runtime.gc();
//			result = compute(stream);
			compute(stream);
			long end = System.currentTimeMillis();
			System.out.println("\tExecution time: \t" + (end - start) + "ms ~ " + (end - start) / 100 + "s");
			
			runtime.gc();
			long memory = totalMemory - runtime.freeMemory();
			
			System.out.println("\tUsed memory: \t\t" + memory + " bytes ~ " + Option.bytesToMegabytes(memory) + " megabytes");
	        
			System.out.println("----------------------------");
			
//			for (Map.Entry<Object, Double> entry : result.entrySet())
//				System.out.println("Item " + entry.getKey() + "\tFreq " + entry.getValue());
		}
	}
	
	/**
	 * To be overridden by sub-class
	 */
	public abstract Map<Object, Double> compute(int[] stream);

}
